var items = new Array();
$(function() {
	$('#addForm').submit(function() {
		$.ajax({
			url: '/wms/cmsmj/saveCmsmj.xhtml',
			data: $('#addForm').serialize(),
			type: "POST",
			dataType : "text",
			error : function() {
				alert('请求失败!');
			},
			success: function(result) {
				if(result=="success") {
					alert("增加成功!");
					closeDialogAdd();
					reloadTable();
				} else {
					alert(result);
				}
			}
		});
		return false;
	});
	$('#updateForm').submit(function() {
		$.ajax({
			url: '/wms/cmsmj/updateCmsmj.xhtml',
			data: $('#updateForm').serialize(),
			type: "POST",
			dataType : "text",
			error : function() {
				alert('请求失败!');
			},
			success: function(result) {
				if(result=="success") {
					alert("修改成功!");
					closeDialogUpdate();
					reloadTable();
				} else {
					alert(result);
				}
			}
		});
		return false;
	});
	ajaxTable();
	setDialogSearch();
	closeDialogSearch();
	setDialogAdd();
	closeDialogAdd();
	setDialogUpdate();
	closeDialogUpdate();
	if(globalMaps.get('2') == undefined) {
		$("#toolbarAdd").hide();
		$("#toolbarForbidden").hide();
		$("#toolbarStartUsing").hide();
	}
});
function ajaxTable() {
	$('#cmsmjTable').datagrid({
		toolbar : [ {
			id : 'toolbarAdd',
			text : '增加',
			iconCls : 'icon-add',
			handler : function() {
				openDialogAdd();
			}
		}, '-', {
			id : 'toolbarForbidden',
			text : '禁用',
			iconCls : 'icon-no',
			handler : function() {
				batch('disable');
			}
		}, '-', {
			id : 'toolbarStartUsing',
			text : '启用',
			iconCls : 'icon-tip',
			handler : function() {
				batch('startUsing');
			}
		}, '-', {
			id : 'toolbarSearch',
			text : '查找',
			iconCls : 'icon-search',
			handler : function() {
				openDialogSearch();
			}
		} ],
		frozenColumns:[[
			{field:"ck", checkbox:true}
		]],
		pageNumber : 1,
		loadMsg : '数据加载中,请稍后...',
		pageList : [ 10, 30, 50, 100 ],
		onLoadError : function() {
			alert('数据加载失败!');
		},
		queryParams : {
			mjaa : $("#searchMjaa").val(),
			mjab : $("#searchMjab").val(),
			mjac : $("#searchMjac").combobox("getValue"),
			mjad : $("#searchMjad").combobox("getValue")
		},
		onClickRow : function(rowIndex, rowData) {
			$('#cmsmjTable').datagrid('unselectRow', rowIndex);
		},
		onDblClickRow : function(rowIndex, rowData) {
			if(globalMaps.get('3') != undefined) {
				openDialogUpdate(rowData.mjaa,rowData.mjab,rowData.mjac,rowData.mjad,rowData.mjae);
			}
	    }
	}).datagrid('acceptChanges');
}
function mjacFormatter(value, row, index) {
	if(row.mjac == 1) {
		return "盘点单";
	} else if(row.mjac == 2) {
		return "调拨单";
	} else if(row.mjac == 3) {
		return "报废单";
	} else if(row.mjac == 4) {
		return "销毁单";
	} else if(row.mjac == 5) {
		return "请购单";
	} else if(row.mjac == 6) {
		return "采购单";
	} else if(row.mjac == 7) {
		return "进货单";
	} else if(row.mjac == 8) {
		return "退货单";
	} else if(row.mjac == 9) {
		return "订单";
	} else if(row.mjac == 10) {
		return "销货单";
	} else if(row.mjac == 11) {
		return "销退单";
	} else if(row.mjac == 12) {
		return "工单";
	} else if(row.mjac == 13) {
		return "领料单";
	} else if(row.mjac == 14) {
		return "退料单";
	} else if(row.mjac == 15) {
		return "入库单";
	} else {
		return "生产入库单";
	}
}
function mjadFormatter(value, row, index) {
	if(row.mjad == "Y") {
		return "自动审核";
	}
	return "手工审核";
}
function mjaeFormatter(value, row, index) {
	if(row.mjae == 2) {
		return "禁用";
	}
	return "正常";
}
function reloadTable() {
	$('#cmsmjTable').datagrid('reload');
	$('#cmsmjTable').datagrid('clearSelections');
}
function setDialogSearch() {
	$('#cmsmjSearch').dialog({
		title : '查询单据类别',
		modal : true,
		left : 50,
		top : 50,
		collapsible : true,
		resizable : true
	});
}
function openDialogSearch() {
	$('#cmsmjSearch').dialog('open');
}
function closeDialogSearch() {
	$('#cmsmjSearch').dialog('close');
}
function searchCmsmj() {
	var queryParams = $('#cmsmjTable').datagrid('options').queryParams;
	queryParams.mjaa = $("#searchMjaa").val();
	queryParams.mjab = $("#searchMjab").val();
	queryParams.mjac = $("#searchMjac").combobox("getValue");
	queryParams.mjae = $("#searchMjad").combobox("getValue");
	reloadTable();
	closeDialogSearch();
}
function setDialogAdd() {
	$('#cmsmjAdd').dialog({
		title : '新增单据类别',
		modal : true,
		left : 50,
		top : 50,
		collapsible : true,
		resizable : true
	});
}
function openDialogAdd() {
	cmsmjAddReset();
	$('#cmsmjAdd').dialog('open');
}
function closeDialogAdd() {
	$('#cmsmjAdd').dialog('close');
}
function cmsmjAdd() {
	var validateResult = $("#addForm").form('validate');
	if(validateResult == false) {
		return false;
	}
	$('#addForm').submit();
}
function cmsmjAddReset() {
	$("#cmsmjAddMjaa").val("");
	$("#cmsmjAddMjab").val("");
	$("#cmsmjAddMjac").combobox("select", "");
	$("#cmsmjAddMjad1").attr("checked", "true");
}
function setDialogUpdate() {
	$('#cmsmjUpdate').dialog({
		title : '修改单据类别',
		modal : true,
		left : 50,
		top : 50,
		collapsible : true,
		resizable : true
	});
}
function openDialogUpdate(mjaa, mjab, mjac, mjad, mjae) {
	items[0] = mjaa;
	items[1] = mjab;
	items[2] = mjac;
	items[3] = mjad;
	items[4] = mjae;
	cmsmjUpdateReset(mjaa, mjab, mjac, mjad, mjae);
	$('#cmsmjUpdate').dialog('open');
}
function closeDialogUpdate() {
	$('#cmsmjUpdate').dialog('close');
}
function cmsmjUpdateReset(mjaa, mjab, mjac, mjad, mjae) {
	$("#cmsmjInfoUpdateMjaa").val(mjaa);
	$("#cmsmjUpdateMjaa").text(mjaa);
	$("#cmsmjUpdateMjab").val(mjab);
	$("#cmsmjUpdateMjac").combobox("select", mjac);
	if(mjad == "N") {
		$("#cmsmjUpdateMjad1").attr("checked", "true");
	} else {
		$("#cmsmjUpdateMjad2").attr("checked", "true");
	}
	if(mjae == 2) {
		$("#cmsmjUpdateMjad").text("禁用");
	} else {
		$("#cmsmjUpdateMjad").text("正常");
	}
}
function cmsmjUpdate() {
	var validateResult = $("#updateForm").form('validate');
	if(validateResult == false) {
		return false;
	}
	$('#updateForm').submit();
}
function disableCmsmh(mjaa) {
	$.messager.confirm('禁用提示', '你确定禁用该单据类别吗?', function(r) {
		if(r) {
			changeStatus('/wms/cmsmj/disableCmsmj.xhtml?mjaa=' + mjaa);
		}
	});
}
function startUsingCmsmh(mjaa) {
	$.messager.confirm('启用提示', '你确定启用该单据类别吗?', function(r) {
		if(r) {
			changeStatus('/wms/cmsmj/startUsingCmsmj.xhtml?mjaa=' + mjaa);
		}
	});
}
function batch(flag) {
	if ($('#cmsmjTable').datagrid('getSelected')) {
		var ids = [];
		var cods = [];
		var selectedRow = $('#cmsmjTable').datagrid('getSelections');
		for ( var i = 0; i < selectedRow.length; i++) {
			ids.push(selectedRow[i].mjaa);
			cods.push(selectedRow[i].mjab);
		}
		var mjaa = ids.join(',');
		if(flag == "disable") {
			$.messager.confirm('禁用提示', '你确定禁用下列单据类别吗?<br/>' + cods.join(','), function(r) {
				if(r) {
					changeStatus('/wms/cmsmj/disableCmsmjBatch.xhtml?mjaa=' + mjaa);
				}
			});
		} else if(flag == "startUsing") {
			$.messager.confirm('启用提示', '你确定启用下列单据类别吗?<br/>' + cods.join(','), function(r) {
				if(r) {
					changeStatus('/wms/cmsmj/startUsingCmsmjBatch.xhtml?mjaa=' + mjaa);
				}
			});
		}
	}
}
function changeStatus(url){
	$.ajax({
		async : false,
		cache : false,
		type : 'POST',
		dataType : "text",
		url : url,
		error : function () {
			alert('请求失败');
		},
		success : function(result){
			if(result=="success") {
				alert("操作成功!");
			} else {
				alert(result);
			}
			reloadTable();
		}
	});
}